package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Description TODO
 * @ClassName RoleController
 * @Author luhui
 * @date 2020.06.13 12:58
 * @version: 1.0
 */

@Controller
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/role/list")
    @RequiredPermission(values = {"角色列表","role:list"})
    public String list(Model model, QueryObject queryObject){
        model.addAttribute("result",roleService.query(queryObject));
        return "role/list";
    }

    @RequestMapping("/role/delete")
    @RequiredPermission(values = {"角色删除","role:delete"})
    public String delete(Long id){
        roleService.delete(id);
        return "redirect:/role/list.do";
    }

    @RequestMapping("/role/input")
    public String input(Model model,Long id){
        if (id != null){
            Role r = roleService.get(id);
            model.addAttribute("r",r);
        }
        model.addAttribute("permissions",permissionService.listAll());
        return "role/input";
    }

    @RequestMapping("/role/saveOrUpdate")
    @RequiredPermission(values = {"角色新增或删除","role:saveOrUpdate"})
    public String saveOrUpdate(Role role,Long[] permissionIds){
        roleService.saveOrUpdate(role,permissionIds);
        return "redirect:/role/list.do";
    }
}